//
//  YWDailyReportCell.m
//  maintain
//
//  Created by kyo on 17/1/5.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWDailyReportCell.h"
#import "UIColor+CustomColors.h"
#import "YWCommonTool.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UILabel+AttributedText.h"

#import "YWReminder.h"

@interface YWDailyReportCell ()
@property (nonatomic, strong) UIView *backGroundIV;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) UILabel *unAssignTroubleLabel;
@property (nonatomic, strong) UILabel *inspectionLabel;
@property (nonatomic, strong) UILabel *keepWatchLabel;
@property (nonatomic, strong) UILabel *detectionLabel;
@property (nonatomic, strong) UILabel *upkeepLabel;
@property (nonatomic, strong) UILabel *troubleMissionLabel;

@property (nonatomic, strong) UILabel *yesterDayFinishMissionLabel;

@property (nonatomic, strong) UILabel *finishTroubleLabel;
@property (nonatomic, strong) UILabel *finishInspectionLabel;
@property (nonatomic, strong) UILabel *finishKeepWatchLabel;
@property (nonatomic, strong) UILabel *finishDetectionLabel;
@property (nonatomic, strong) UILabel *finishUpkeepLabel;

@property (nonatomic, strong) UIButton *shareButton;
@end

@implementation YWDailyReportCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor customBackgroudColor];
        [self autolayoutViews];
    }
    return self;
}

- (void)setReminder:(YWReminder *)reminder{
    _reminder = reminder;
    self.dateLabel.text = [YWCommonTool showTime:reminder.createTime/1000 andFormatter:KTimeFormatShortTime];
    self.titleLabel.text = reminder.title?:NSLocalizedString(@"reminderTypeOfDailyRport", nil);
    self.contentLabel.text = NSLocalizedString(@"todayWorks", nil);//@"今日待处理事项";//reminder.content?:@"今日待处理事项";
    
    NSString *countString = [NSString stringWithFormat:@"%ld%@",(long)reminder.todoDesignTroubleCount,NSLocalizedString(@"tiao", nil)];
    [self.unAssignTroubleLabel labelText:NSLocalizedString(@"unassignedTrouble", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    countString = [NSString stringWithFormat:@"%ld%@",(long)reminder.todoInspectionMissionCount,NSLocalizedString(@"tiao", nil)];
    [self.inspectionLabel labelText:NSLocalizedString(@"inspectionMissionFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    countString = [NSString stringWithFormat:@"%ld%@",(long)reminder.todoKeepWatchMissionCount,NSLocalizedString(@"tiao", nil)];
    [self.keepWatchLabel labelText:NSLocalizedString(@"keepWatchMissionFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    countString = [NSString stringWithFormat:@"%ld%@",(long)reminder.todoDetectionCount,NSLocalizedString(@"ge", nil)];
    [self.detectionLabel labelText:NSLocalizedString(@"detectionEquipmentFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    countString = [NSString stringWithFormat:@"%ld%@",(long)reminder.todoUpkeepCount,NSLocalizedString(@"ge", nil)];
    [self.upkeepLabel labelText:NSLocalizedString(@"upkeepEquipmentFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    countString = [NSString stringWithFormat:@"%ld%@",(long)reminder.todoFixTroubleCount,NSLocalizedString(@"tiao", nil)];
    [self.troubleMissionLabel labelText:NSLocalizedString(@"troubleMissionFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    self.yesterDayFinishMissionLabel.text = NSLocalizedString(@"yestodayWorks", nil);//@"昨日已完成事项";
    
    countString = [NSString stringWithFormat:@"%ld%@",(long)reminder.finishFixTroubleCount,NSLocalizedString(@"tiao", nil)];
    [self.finishTroubleLabel labelText:NSLocalizedString(@"finishTroubleFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    countString = [NSString stringWithFormat:@"%ld次",(long)reminder.finishInspectionCount];
    [self.finishInspectionLabel labelText:NSLocalizedString(@"finishInspectionFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    countString = [NSString stringWithFormat:@"%ld次",(long)reminder.finishKeepWatchCount];
    [self.finishKeepWatchLabel labelText:NSLocalizedString(@"finishKeepWatchFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    countString = [NSString stringWithFormat:@"%ld%@",(long)reminder.finishDetectionCount,NSLocalizedString(@"ge", nil)];
    [self.finishDetectionLabel labelText:NSLocalizedString(@"detectionEquipmentFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    countString = [NSString stringWithFormat:@"%ld%@",(long)reminder.finishUpkeepCount,NSLocalizedString(@"ge", nil)];
    [self.finishUpkeepLabel labelText:NSLocalizedString(@"upkeepEquipmentFormatter", nil) withAttributedText:countString andAttributedColor:[UIColor blackColor]];
    
    self.contentLabel.preferredMaxLayoutWidth = self.contentLabel.frame.size.width;
}

- (void)autolayoutViews{
    UIView *superView = self.contentView;
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(superView.mas_top).offset(kTopPadding);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(kMinLabelH);
    }];
    [self.backGroundIV mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_dateLabel.mas_bottom).offset(kMarginW);
        make.left.mas_equalTo(superView.mas_left).offset(kMarginW);
        make.right.mas_equalTo(superView.mas_right).offset(-kMarginW);
        make.bottom.mas_equalTo(superView.mas_bottom).offset(-kMarginW);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_backGroundIV.mas_top).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(kMarginW);
        //make.right.mas_equalTo(_backGroundIV.mas_right).offset(-kMarginW);
    }];
    
    [self.shareButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_backGroundIV.mas_top).offset(8);
        make.left.mas_equalTo(_titleLabel.mas_right).offset(kMarginW);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-kMarginW);
        make.width.mas_equalTo(@32);
        make.height.mas_equalTo(@32);
    }];
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_titleLabel.mas_left);
        make.right.mas_equalTo(_titleLabel.mas_right);
    }];
    [self.unAssignTroubleLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_contentLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_contentLabel.mas_left);
        make.right.mas_equalTo(superView.mas_centerX).offset(-1);
    }];
    [self.troubleMissionLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_unAssignTroubleLabel.mas_top);
        make.left.mas_equalTo(superView.mas_centerX).offset(kMarginW);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-kMarginW);
    }];
    [self.keepWatchLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_unAssignTroubleLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_unAssignTroubleLabel.mas_left);
        make.right.mas_equalTo(_unAssignTroubleLabel.mas_right);
    }];
    [self.inspectionLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_troubleMissionLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_troubleMissionLabel.mas_left);
        make.right.mas_equalTo(_troubleMissionLabel.mas_right);
    }];
    [self.detectionLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_keepWatchLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_keepWatchLabel.mas_left);
        make.right.mas_equalTo(_keepWatchLabel.mas_right);
    }];
    [self.upkeepLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_inspectionLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_inspectionLabel.mas_left);
        make.right.mas_equalTo(_inspectionLabel.mas_right);
    }];
    
    [self.yesterDayFinishMissionLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_upkeepLabel.mas_bottom).offset(kTopPadding);
        make.left.mas_equalTo(_contentLabel.mas_left);
        make.right.mas_equalTo(_contentLabel.mas_right);
    }];
    [self.finishTroubleLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_yesterDayFinishMissionLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(kMarginW);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-kMarginW);
    }];
    [self.finishKeepWatchLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_finishTroubleLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_finishTroubleLabel.mas_left);
        make.right.mas_equalTo(_keepWatchLabel.mas_right);
    }];
    [self.finishInspectionLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_finishTroubleLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_inspectionLabel.mas_left);
        make.right.mas_equalTo(_inspectionLabel.mas_right);
    }];
    [self.finishDetectionLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_finishKeepWatchLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_finishKeepWatchLabel.mas_left);
        make.right.mas_equalTo(_finishKeepWatchLabel.mas_right);
        make.bottom.mas_equalTo(_backGroundIV.mas_bottom).offset(-kTopPadding);
    }];
    [self.finishUpkeepLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(_finishKeepWatchLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_finishInspectionLabel.mas_left);
        make.right.mas_equalTo(_finishInspectionLabel.mas_right);
        make.bottom.mas_equalTo(_backGroundIV.mas_bottom).offset(-kTopPadding);
    }];
    
}

- (UILabel *)dateLabel{
    if (!_dateLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor customTextGrayColor];
        [self.contentView addSubview:label];
        _dateLabel = label;
    }
    return _dateLabel;
}

- (UIView *)backGroundIV{
    if (!_backGroundIV) {
        UIView *backGroundIV = [[UIView alloc] init];
        backGroundIV.layer.masksToBounds = YES;
        backGroundIV.layer.cornerRadius = 4.0;
        backGroundIV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:backGroundIV];
        _backGroundIV = backGroundIV;
    }
    return _backGroundIV;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = KTitleFont;
        [self.backGroundIV addSubview:label];
        _titleLabel = label;
    }
    return _titleLabel;
}

- (UILabel *)contentLabel{
    if (!_contentLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _contentLabel = label;
    }
    return _contentLabel;
}

- (UILabel *)unAssignTroubleLabel{
    if (!_unAssignTroubleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _unAssignTroubleLabel = label;
    }
    return _unAssignTroubleLabel;
}

- (UILabel *)inspectionLabel{
    if (!_inspectionLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _inspectionLabel = label;
    }
    return _inspectionLabel;
}

- (UILabel *)keepWatchLabel{
    if (!_keepWatchLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _keepWatchLabel = label;
    }
    return _keepWatchLabel;
}

- (UILabel *)detectionLabel{
    if (!_detectionLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _detectionLabel = label;
    }
    return _detectionLabel;
}

- (UILabel *)upkeepLabel{
    if (!_upkeepLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _upkeepLabel = label;
    }
    return _upkeepLabel;
}

- (UILabel *)troubleMissionLabel{
    if (!_troubleMissionLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _troubleMissionLabel = label;
    }
    return _troubleMissionLabel;
}

- (UILabel *)yesterDayFinishMissionLabel{
    if (!_yesterDayFinishMissionLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _yesterDayFinishMissionLabel = label;
    }
    return _yesterDayFinishMissionLabel;
}

- (UILabel *)finishTroubleLabel{
    if (!_finishTroubleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _finishTroubleLabel = label;
    }
    return _finishTroubleLabel;
}

- (UILabel *)finishInspectionLabel{
    if (!_finishInspectionLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _finishInspectionLabel = label;
    }
    return _finishInspectionLabel;
}

- (UILabel *)finishKeepWatchLabel{
    if (!_finishKeepWatchLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _finishKeepWatchLabel = label;
    }
    return _finishKeepWatchLabel;
}

- (UILabel *)finishDetectionLabel{
    if (!_finishDetectionLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _finishDetectionLabel = label;
    }
    return _finishDetectionLabel;
}

- (UILabel *)finishUpkeepLabel{
    if (!_finishUpkeepLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayColor];
        [self.backGroundIV addSubview:label];
        _finishUpkeepLabel = label;
    }
    return _finishUpkeepLabel;
}

- (UIButton *)shareButton{
    if (!_shareButton) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"common_share"] forState:UIControlStateNormal];
        button.titleLabel.font = kContentFont;
        button.imageEdgeInsets = UIEdgeInsetsMake(4, 4, 4, 4);
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickShareButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.backGroundIV addSubview:button];
        _shareButton = button;
    }
    return _shareButton;
}

- (void)clickShareButtonEvent:(id)sender{
    if ([self.delegate respondsToSelector:@selector(handleShareButtonEvent:)]) {
        [self.delegate handleShareButtonEvent:self];
    }
}


@end
